﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZOVNP.Model.Abstracts;

namespace ZOVNP.Model
{
    public class Workspace
    {
        private IDomainEngine _engine;
        public Workspace(IDomainEngine engine)
        {
            _engine = engine;
        }

        public IDomainEngine Engine
        {
            get { return _engine; }
        }

        #region Get
        public IEnumerable<IMaterial> GetAllMaterials()
        {
            IMaterial[] result;
            using (var context = _engine.CreateContext())
            {
                result = context.Query<IMaterial>().ToArray();
            }
            return result;
        }

        public IEnumerable<IPrice> GetAllPrices()
        {
            IPrice[] result;
            using (var context = _engine.CreateContext())
            {
                result = context.Query<IPrice>().ToArray();
            }
            return result;
        }

        public IEnumerable<IProductType> GetAllProductTypes()
        {
            IProductType[] result;
            using (var context = _engine.CreateContext())
            {
                result = context.Query<IProductType>().ToArray();
            }
            return result;
        }

        public IEnumerable<IProduct> GetAllProducts()
        {
            IProduct[] result;
            using (var context = _engine.CreateContext())
            {
                result = context.Query<IProduct>().ToArray();
            }
            return result;
        }

        public IEnumerable<IUser> GetAllUsers()
        {
            IUser[] result;
            using (var context = _engine.CreateContext())
            {
                result = context.Query<IUser>().ToArray();
            }
            return result;
        }

        public IEnumerable<ICustomer> GetAllCustomers()
        {
            ICustomer[] result;
            using (var context = _engine.CreateContext())
            {
                result = context.Query<ICustomer>().ToArray();
            }
            return result;
        }

        public IEnumerable<IOrderPartHelp> GetAllOrderPartHelps()
        {
            IOrderPartHelp[] result;
            using (var context = _engine.CreateContext())
            {
                result = context.Query<IOrderPartHelp>().ToArray();
            }
            return result;
        }

        public IEnumerable<IOrderPart> GetAllOrderParts()
        {
            IOrderPart[] result;
            using (var context = _engine.CreateContext())
            {
                result = context.Query<IOrderPart>().ToArray();
            }
            return result;
        }

        public IEnumerable<IPriceItem> GetAllPriceItems()
        {
            IPriceItem[] result;
            using (var context = _engine.CreateContext())
            {
                result = context.Query<IPriceItem>().ToArray();
            }
            return result;
        }

        public IEnumerable<IPriceItem> GetAllPriceItemsFull()
        {
            IPriceItem[] result;
            using (var context = _engine.CreateContext())
            {
                result = context.Query<IPriceItem>().Include(pi => pi.Material).Include(pi => pi.Product).ToArray();
            }
            return result;
        }

        public IEnumerable<IOrder> GetAllOrders()
        {
            IOrder[] result;
            using (var context = _engine.CreateContext())
            {
                result = context.Query<IOrder>().ToArray();
            }
            return result;
        }

        public IEnumerable<IProduct> GetProductsOfType(Guid productTypeId)
        {
            IProduct[] result;
            using (var context = _engine.CreateContext())
            {
                result = context.Query<IProduct>().Where(p => p.ProductType.ID == productTypeId).ToArray();
            }
            return result;
        }

        public IEnumerable<IPriceItem> GetPriceItemsOfType(Guid productTypeId)
        {
            IPriceItem[] result;
            using (var context = _engine.CreateContext())
            {
                result = context.Query<IPriceItem>().Where(p => p.Product.ProductType.ID == productTypeId).ToArray();
            }
            return result;
        }

        public IEnumerable<IPriceItem> GetPriceItemsOfProduct(Guid productId)
        {
            IPriceItem[] result;
            using (var context = _engine.CreateContext())
            {
                result = context.Query<IPriceItem>().Where(p => p.Product.ID == productId).ToArray();
            }
            return result;
        }

        #endregion

        public ICustomer CreateCustomer()
        {
            ICustomer result = _engine.Adder.CreateCustomer();
            return result;
        }

        public void Insert(IDomainObject domainObject)
        {
            using(var context = _engine.CreateContext())
            {
                context.AttachObject(domainObject);
                context.SaveChanges();
            }
        }

        public IOrderItem CreateOrderItem()
        {
            IOrderItem result = _engine.Adder.CreateOrderItem();
            return result;
        }

        public IOrderPart CreateOrderPart()
        {
            IOrderPart result = _engine.Adder.CreateOrderPart();
            return result;
        }

        public IOrder CreateOrder()
        {
            IOrder result = _engine.Adder.CreateOrder();
            return result;
        }
    }
}
